﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using System.Reflection;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Doc
{
    public partial class List : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        
        protected KindLogic _kindLogic;

        public string _kind;
        public string _typeid;
        public string _title;
        public string _acthor;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _kind = String.IsNullOrEmpty(Request["kind"]) ? "" : Request["kind"];

            _title = String.IsNullOrEmpty(Request["doctitle"]) ? "" : Request["doctitle"];
            _acthor = String.IsNullOrEmpty(Request["acthor"]) ? "" : Request["acthor"];
            _typeid = String.IsNullOrEmpty(Request["typeid"]) ? "" : Request["typeid"];

            //初始化控件值
            ParamKit.Bind("TreeDefType", ddl_typeid, new List<ParamFilterCondition> { new ParamFilterCondition() { Code = "Kind", Value = _kind } }, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");

            tb_search_kind.Text = _kind;
			
            tb_search_title.Text = _title;
            tb_search_author.Text = _acthor;
            ddl_typeid.SelectedValue = _typeid;

            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            _kindLogic = new KindLogic(_kind);

            if (_framePage.IsAjaxPagerPostBack)
            {
                BindList(_framePage.PageIndex, _framePage.PageSize);
            }
            else
            {
                BindList(1, 20);
            }
        }

        /// <summary>
        /// 获取config的属性值
        /// </summary>
        /// <returns></returns>
        protected bool GetConfigPropertyValue(String field)
        {
            PropertyInfo property = _kindLogic.Config.GetType().GetProperty(field);
            return (bool)property.GetValue(_kindLogic.Config, null);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<CmsDocInfo>();
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).AddOrder(Order.Desc("UpdateDate"));

                gridArticle.DataSource = criteria.List();
                gridArticle.DataBind();
            }
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<CmsDocInfo>();
                SetupCondition(ref criteria);
                return (int)criteria.SetProjection(Projections.RowCount()).UniqueResult();
            }
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="crt"></param>
        protected void SetupCondition(ref ICriteria crt)
        {
            crt.Add(Expression.Eq("Kind", _kind));

            if (!string.IsNullOrEmpty(_title.Trim()))
            {
                crt.Add(Expression.Like("Title", "%" + _title + "%"));
            }

            if (!string.IsNullOrEmpty(_acthor.Trim()))
            {
                crt.Add(Expression.Like("Author", "%" + _acthor + "%"));
            }

            if (!string.IsNullOrEmpty(_typeid.Trim()) && _typeid.Trim() != "-1")
            {
                crt.Add(Expression.Eq("TypeId", int.Parse(_typeid)));
            }
        }
    }
}
